package com.mediaFeed.dao.hibernate;

import com.mediaFeed.dao.VideoDao;
import com.mediaFeed.model.Video;

import java.util.List;

import org.springframework.orm.ObjectRetrievalFailureException;

/**
 * Author:Louis Majanja <louis@media.berkeley.edu>
 * Date: Nov 13, 2006
 * Time: 3:24:29 PM
 */
public class VideoDaoHibernate extends BaseDaoHibernate implements VideoDao {

    public Video getVideo(Long videoId) {
           Video video = (Video) getHibernateTemplate().get(Video.class, videoId);

        if (video == null) {
            log.warn("uh oh, video '" + videoId + "' not found...");
            throw new ObjectRetrievalFailureException(Video.class, videoId);
        }

        return video;
    }

    public void saveVideo(Video video) {

        if (log.isDebugEnabled()) {
            log.debug("video's id: " + video.getId());
        }

        getHibernateTemplate().saveOrUpdate(video);
        // necessary to throw a DataIntegrityViolation and catch it in UserManager
        getHibernateTemplate().flush();
    }

    public void removeVideo(Long videoId) {
        getHibernateTemplate().delete(getVideo(videoId));
    }

    public List getVideo(Video video) {
        return getHibernateTemplate().find("from Video v order by upper(v.title)");
    }
}
